module.exports = function(lab) {
    return {
        "updateUserInfo": function(session,account) {
            return @.async(function() {
                let account_id = session.currentUser.id;
                return lab.find("contribute_user_info",{
                    account_id
                }).then(function(info){
                    if(info) {
                        return lab.update("contribute_user_info",Object.assign(info,{
                            "info": account,
                            "e_mail": account.email,
                            "phone": account.phone
                        }))
                    } else {
                        return lab.create("contribute_user_info",{
                            "info":account,
                            account_id,
                            "e_mail": account.email,
                            "phone": account.phone
                        })
                    }
                })
            })
        },
        "getUserInfo":function(session) {
            return lab.find("contribute_user_info",{
                "account_id":session.currentUser.id
            })
        },
        "getUsers":function(session) {

            return lab.query("contribute_user_info",{
                start: 0,
                limit: -1,
                mapper: (record) => {
                    record.info = JSON.parse(record.info)
                    return record;
                }
            }).then(function({list}){
                this.next(list)
            })
        },
        "sendMessage":function(session,message) {
            return lab.create("contribute_messages",Object.assign(message,{
                "sender_id": session.currentUser.id,
                "create_date": Date.now()
            }))
        },
        "getMessages": function(session,{start = 0,limit = 10}) {
            return lab.query("contribute_messages",{
                "conditions" : ["addressee_id = ?","is_removed = ?"],
                "params": [session.currentUser.id, false],
                "start": start,
                "limit": limit,
                "sort": "create_date desc"
            }).then(function({ list, total }) {
                return @.async.all(list,function(item){
                    return lab.find("contribute_user_info",{
                        "account_id":item.sender_id
                    }).then(function(result){
                        item.sender_name = result.info.username;
                        this.next(item);
                    })
                }).then(function(result){
                    this.next({
                        "list":result,
                        total
                    })
                })
            })
        },
        "removeMessageByID":function(session,id) {
            return lab.find("contribute_messages",id).then(function(result){
                return lab.update("contribute_messages",Object.assign(result,{
                    "is_removed" : true
                }))
            })
        }
    }
}